package com.webcamtracker.utils;

import com.webcamtracker.configuration.RootPref;
import com.webcamtracker.image.color.hsv.HSVColor;
import com.webcamtracker.image.color.hsv.HSVColorSampler;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;


public class Hits extends ArrayList<Hit> {
    private int totalElements = 0;


    public Hits() {
    }


    public static Hits filter(Hits hitList) {
        Hits result = new Hits();
        double i = 0;
        int totalHits = 0;
        double percentenge = RootPref.getTargetPref().getDouble(RootPref.KEY_MAX_TOTAL_PERCENTENGE, RootPref.DEF_VALUE_MAX_TOTAL_PERCENTENGE);
        double defValueMinimumPercentage = RootPref.getTargetPref().getDouble(RootPref.KEY_MINIMUM_PERCENTAGE, RootPref.DEF_VALUE_MINIMUM_PERCENTAGE);
        for (Iterator it = hitList.iterator(); it.hasNext() && i < percentenge;) {
            Hit hit = (Hit) it.next();
            float percentage = hit.getPercentage(hitList);
            if (percentage > defValueMinimumPercentage) {
                i += percentage;
                result.add(hit);
                totalHits += hit.getHits();
            }
        }
        result.totalElements = totalHits;
        return result;
    }

    public static Hits buildFromImage(BufferedImage targetModelImage) {
        final int w = targetModelImage.getWidth();
        final int h = targetModelImage.getHeight();
        Hits hits = new Hits();

        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                HSVColorSampler hsvColorSampler = new HSVColorSampler();
                HSVColor hsv = (HSVColor) hsvColorSampler.getColorAt(targetModelImage, i, j);
//                boolean saturaion = hsv.getSaturation() > RootPref.getTargetPref().getDouble(RootPref.KEY_MIN_SATURATION, RootPref.DEF_VALUE_MIN_SATURATION);
//                boolean brightness = hsv.getBrightness() > RootPref.getTargetPref().getDouble(RootPref.KEY_MIN_BRIGHTNESS, RootPref.DEF_VALUE_MIN_BRIGHTNESS);
//                if (saturaion && brightness) {
                    Hit hit = new Hit(hsv);
                    hits.newElement();

                    int i1 = hits.indexOf(hit);

                    if (i1 >= 0) {
                        Hit old = hits.get(i1);
                        old.hit(hsv);
                    } else {
                        hits.add(hit);
                        hit.hit(hsv);
                    }
                    hits.newElement();
//                }
            }

        }

        Collections.sort(hits, new Comparator<Hit>() {

            public int compare(Hit o1, Hit o2) {
                return o2.getHits() - o1.getHits();
            }
        });

        return hits;
    }

    public void newElement() {
        totalElements++;
    }


    public int getTotalSamples() {
        return totalElements;
    }


    public String toString() {
        return "Hits{" +
                "totalElements=" + totalElements + ", Elements = " + super.toString() +
                '}';
    }

    public Hits newInstance() {
        Hits hits = new Hits();
        for (Hit hit : this) {
            hits.add(new Hit(hit.getColor(), false));
        }
        return hits;
    }
}
